from db_handler import MysqlHander
from my_http import MyHttp
import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re



class SHCode:
    def __init__(self):
        self.db = MysqlHander("config.ini")
        
    def gupiao_insert(self, code, name):
        trip_code = code.strip()
        trip_name = name.strip()
        sql = "insert into entity_gupiao (Fcode, Fname, Fexchange_id, Fcreate_time, Fmodify_time) values "
        sql += "('" + trip_code + "','" + trip_name + "',1 , now(), now());"
        #print(sql)
        insert = 0
        try:
            self.db.execute_not_safe(sql)
            insert = 1
        except Exception as e:
            pass
        if 1 == insert:
            return
        update_sql = "update entity_gupiao set Fmodify_time=now(),Fexchange_id=1,Fname='" + trip_name + "' where Fcode='" + trip_code + "';"
        print(update_sql)
        try:
            self.db.execute_not_safe(update_sql)
        except Exception as e:
            pass
        
    def get_http_data(self):
        url = "http://www.sse.com.cn/market/sseindex/indexlist/s/i000002/const_list.shtml"
        bs4_data = MyHttp.bs4_utf8_data(url)
        datas = bs4_data.find_all(name="table", attrs={'class':'tablestyle'})
        for d in datas:
            sh_codes = d.find_all(name="a")
            for sh_code in sh_codes:
                fileds = re.split('[（）()]', sh_code.text)
                if 3 == len(fileds):
                    self.gupiao_insert(fileds[1], fileds[0])
    def get_file_data(self, file):
        with open(file, 'r', encoding='utf-8') as f:
            self.data = f.readlines()
        for d in self.data:
            ds = d.split("|")
            self.gupiao_insert(ds[0], ds[1])

if __name__ == '__main__':
    code = SHCode()
    #code.get_http_data()
    code.get_file_data("./out/shangjiaosuo_stock.txt")